﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using DiscoveryLogic.Analytics.DataObjects.Networks;
using DiscoveryLogic.Analytics.UI.Common.Events;
using DiscoveryLogic.Common.Utilities.TextMatch;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Wpf.Events;

namespace DiscoveryLogic.Analytics.Modules.Clusters
{
    public class TagCloudPresenter
    {
        private TagCloud _View;
		/// <summary></summary>
		public TagCloud View
		{
			get
			{
				return this._View;
			}
			set
			{
				this._View = value;
				if(this._View !=null)
				{
                    this._View.TagSelectionChanged += new TagCloud.OnTagSelectionChanged(SelectTag);
				}
			}
		}

        private readonly IEventAggregator _EventAggregator;
        private SubscriptionToken _SubscriptionToken;

        public TagCloudPresenter(IEventAggregator eventAggregator)
		{
			this._EventAggregator = eventAggregator;
		}

        public void SelectTag(TermWeight tw)
        {
            if (tw != null)
            {
                this._EventAggregator.GetEvent<TagSelectedEvent>().Publish(tw);
            }
        }

        public void RegisterTagSelectionChangedEventHandler(TermWeight tw)
        {
            NetworkSelectedEvent networkSelectedEvent =
                    _EventAggregator.GetEvent<NetworkSelectedEvent>();
            if (this._SubscriptionToken != null)
            {
                networkSelectedEvent.Unsubscribe(_SubscriptionToken);
            }
            this._SubscriptionToken = networkSelectedEvent.Subscribe(
                NetworkSelectedHandler,
                ThreadOption.UIThread,
                false);
        }

        private void NetworkSelectedHandler(Network network)
        {
            // update network 
            network = Network.Read(network.NetworkName);
            Dictionary<int, NetworkCluster> clusters = NetworkCluster.Read(network.NetworkName);
            //Dictionary<string,double> termWeights=new Dictionary<string, double>();
            //if(network !=null && clusters !=null && clusters.Count>0)
            //{
            //    foreach(NetworkCluster cluster in clusters.Values)
            //    {
            //        if(cluster.KeyIndicators !=null && cluster.KeyIndicators.Count>0)
            //        {
            //            foreach(string term in cluster.KeyIndicators.Keys)
            //            {
            //                if(!termWeights.ContainsKey(term))
            //                    termWeights.Add(term,cluster.KeyIndicators[term]);
            //            }
            //        }
            //    }
            //}
            //List<TermUsage> tus = new List<TermUsage>();
            //foreach(string term in termWeights.Keys)
            //{
            //    TermUsage tu = new TermUsage();
            //    tu.Term = term;
            //    tu.Weight = termWeights[term];
            //    tus.Add(tu);
            //}
            //tus.Sort();
            //List<TermWeight> tws=new List<TermWeight>();
            //foreach(TermUsage tu in tus)
            //{
            //    TermWeight tw=new TermWeight();
            //    tw.Term = tu.Term;
            //    tw.Weight = tu.Weight;
            //    tws.Add(tw);
            //}
            //this._View.TermWeights = tws;
        }
    }

    /// <summary>
    /// sorting by term alphabetically
    /// </summary>
    public class TermUsage:IComparable
    {
        private string _Term;
        /// <summary></summary>
        public string Term
        {
            get
            {
                return this._Term;
            }
            set
            {
                this._Term = value;
            }
        }

        private double _Weight;
        /// <summary></summary>
        public double Weight
        {
            get
            {
                return this._Weight;
            }
            set
            {
                this._Weight = value;
            }
        }

        #region Implementation of IComparable
        public int CompareTo(object obj)
        {
            if(obj.GetType()==typeof(TermUsage))
            {
                TermUsage tu = (TermUsage) obj;
                return this.Term.CompareTo(tu.Term);
            }
            return 0;
        }

        #endregion
    }
}
